<?php

namespace App\Http\Controllers;

use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesResources;
use App\AppToken;
class Controller extends BaseController
{
    use AuthorizesRequests, AuthorizesResources, DispatchesJobs, ValidatesRequests;
    
    protected function addPicture($base64code) {

        $dir = 'uploads/'.date('Y-m-d');
        if (!is_dir($dir))
            mkdir($dir);

        $imgData=base64_decode($base64code);
        $filename = $dir.'/'.str_random(6).'.jpg';
        file_put_contents($filename, $imgData);

        return $filename;
    }

    protected function validatorError($validator) {
        echo json_encode(['status' => 403, 'errmsg' => $validator->errors(), 'data' => []]);
        return ;
    }

    protected function sessionValidate( $sessionId) {
        $token = new AppToken();
        $result = $token->ValidateSession($sessionId);
        if ($result == false) {
            echo json_encode(['status' => 403, 'errmsg' => 'sessionId not found', 'data' => []]);
            return false;
        }
        return true;
    }

    protected function returnEcho($status = 200, $errmsg = '', $data = '') {
        echo json_encode(['status' => $status, 'errmsg' => $errmsg, 'data' => $data]);
    }
}

